//===============================================================================
//==
//==
//==
//==
//===============================================================================

//#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <string.h>
#include <gl/glew.h>
#include "ksys.h"
#include "appbase.h"
#include "gfxutils.h"

//===============================================================================
char	_appbase_window_title[] = "aibocap1";
char	_appbase_window_class[] = "AIBOCAP1_CLASS";

//===============================================================================
static console_t	_console;

//===============================================================================
static int		_disp_w, _disp_h;
static win_t	*_mainwinp;

//===============================================================================
static void reshape( int w, int h )
{
float	fov = 70;

	_disp_w = w;
	_disp_h = h;

	glViewport( 0, 0, w, h );
	glMatrixMode( GL_PROJECTION );
		glLoadIdentity();

	glMatrixMode( GL_MODELVIEW );
}

//===============================================================================
/*
void appbase_yeld(void)
{
compak_inpack_t *inpackp;

	idle_no_packet_process();

	if ( inpackp = compak_in_packet_get( &_cpk ) )
	{
		process_input_packets( inpackp );
		compak_in_packet_dispose( &_cpk );
	}
}
*/
/*
//==================================================================
static void cmd_capsetup( void *userp, char *params[], int n_params )
{
	_cap.driver_dialog_videosource();
}
//==================================================================
static cons_cmd_def_t	_cmd_defs[] =
{
"/connect",			NULL, cmd_connect			,"/connect <IP>          : connect",
//"/videosetup",		NULL, cmd_videosetup		,"/videosetup            : video source setup",
"/capdevice",		NULL, cmd_capdevice			,"/capdevice <n>         : select a video device",
"/capsetup",		NULL, cmd_capsetup			,"/capsetup              : ",
"/dumpframe",		NULL, cmd_dumpframe			,"/dumpframe <filename>  : ",
0
};

//===============================================================================
static void console_line_func( const char *txtp, int is_cmd )
{
	if ( _state_flags & STATE_COMMUNICATING )
	{
	int				err;
	packet_header_t	head;

		if ( txtp[0] )
		{
			head.id = RDP_TMSG_PKID;
			err = compak_send( &_cpk, &head, sizeof(head), txtp, (strlen( txtp )+1), NULL );

			KASSERT( err == 0 );
		}
	}
}
*/

//===============================================================================
static int init( win_t *main_winp )
{
	_mainwinp = main_winp;

//	ss_init_winsock();

	//-----------------------------------------------
	FONT_Create();

	//-----------------------------------------------
	cons_init( &_console, main_winp );
	cons_select( &_console );
	cons_line_cb_set( console_line_func );
	cons_line_printf( "%s -- %s, %s", _appbase_window_title, __DATE__, __TIME__ );
	cons_line_printf( "Type /help for help" );

	cons_cmd_add_defs( _cmd_defs );

	cons_show( 1 );
//	cons_cmd_execute( "/capdevice 0" );

	return 0;
}

//===============================================================================
extern "C" void main_anim(void)
{
//	appbase_yeld();

	Sleep( 2 );
}

//===============================================================================
extern "C" void main_quit(void)
{
}

//===============================================================================
extern "C" int	main_requestquit(void)
{
	return 0;
}

//===============================================================================
static void do_paint( win_t *winp )
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glDisable( GL_LIGHTING );

	begin_2D( 0, 0 );

//		vchat_redraw_him( winp->w, winp->h );
//		vchat_redraw_me( winp->w, winp->h );

//		debugout_render();

	end_2D();
}

//===============================================================================
extern "C" int main_eventfilter( void *userobjp, WindowEvent::Type etype, Window::Event *eventp )
{
//	mtrack1_eventfilter( &_tracker, etype, eventp );

	switch ( etype )
	{
	case WindowEvent::ETYPE_CREATE:
		init( eventp->winp );
		break;

	case WindowEvent::ETYPE_WINRESIZE:
		reshape( eventp->win_w, eventp->win_h );
		break;

	case WindowEvent::ETYPE_PAINT:
		do_paint( eventp->winp );
		break;
	}

	return 0;
}

//===============================================================================
extern "C" int	main_windows_event_filter( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
	return 0;
}
